<template>
  <div class="rh-input">
    <input :type="type"
           v-if="type!=='textarea'"
           :placeholder="placeholder"
           v-model="newvalue"
           :disabled="disabled"
           :autocomplete="autocomplete"
           :autofocus="autofocus"
           :readonly="readonly"
           ref="rh-input"
           @blur="blur"
           @focus="focus"
           @change="change"
           @input="input">
    <textarea v-if="type==='textarea'"
              :rows="rows"
              :placeholder="placeholder"
              v-model="newvalue"
              :disabled="disabled"
              :autocomplete="autocomplete"
              :autofocus="autofocus"
              @blur="blur"
              @focus="focus"
              @change="change"
              @input="input"></textarea>
    <span v-if="!!maxlength">{{ newvalue.length }}/{{ maxlength }}</span>
  </div>
</template>

<script>
export default {
  data () {
    return {
      newvalue: this.value
    }
  },
  mounted () {
    if (['medium', 'small', 'mini'].indexOf(this.size) !== -1) {
      this.$refs['rh-input'].classList.add(this.size)
    }
  },
  watch: {
    newvalue (value) {
      if (value.length > this.maxlength) {
        this.newvalue = value.slice(0, this.maxlength);
      }
      this.$emit('input', this.newvalue);
    },
    value (value) {
      this.$refs['rh-input'].value = value;
    }
  },
  props: {
    type: { // 类型
      type: String,
      default: 'text'
    },
    value: {
      default: ''
    },
    maxlength: { // 最大输入长度
      type: Number
    },
    placeholder: { // 输入框占位文本
      type: String
    },
    disabled: { // 禁用
      type: Boolean,
      default: false
    },
    size: { // 输入框尺寸
      type: String
    },
    rows: { // 输入框行数
      type: Number,
      default: 3
    },
    autocomplete: { // 自动补全
      type: String,
      default: 'off'
    },
    autofocus: { // 自动获取焦点
      type: Boolean,
      default: false
    },
    readonly: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    blur () {
      this.$emit('blur')
    },
    focus () {
      this.$emit('focus')
    },
    change () {
      this.$emit('change')
    },
    input () {
      this.$emit('input')
    }
  }
}
</script>

<style lang="less" scoped>
.rh-input {
  width: 100%;
  position: relative;

  input,
  textarea {
    color: #606266;
    font-size: 14px;
    width: 100%;
    border: 1px solid #dcdfe6;
    box-sizing: border-box;
    outline: none;
    border-radius: 5px;
    transition: all 0.5s ease-in-out;
  }
  input {
    height: 40px;
    padding: 0 15px;
  }
  textarea {
    padding: 5px 15px;
  }
  input:hover,
  textarea:hover {
    border: 1px solid #c0c4cc;
  }
  input:focus,
  textarea:focus {
    border-color: #f10;
  }
  input::placeholder,
  textarea::placeholder {
    color: #c0c4cc;
  }
  input:disabled,
  input:read-only,
  textarea:read-only,
  textarea:disabled {
    background-color: #fff;
    cursor: pointer;
  }
  span {
    height: 40px;
    width: 40px;
    line-height: 40px;
    position: absolute;
    font-size: 12px;
    color: #909399;
    right: 0;
    bottom: 0;
  }
}
.medium {
  height: 36px;
}
.small {
  height: 32px;
}
.mini {
  height: 28px;
}
</style>